﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.users
{
    public partial class cur_list : Web.UI.ManagePage
    {
        Model.manager mgr;
        string appWhere;
        protected string dis = "display:none;";
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");

            this.pageSize = GetPageSize(6); //每页数量
            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                ChkAdminLevel("sys_customer_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                if (string.IsNullOrEmpty(Session[DTKeys.SESSION_TEMPLATES_INFO].ToString()))
                {
                    JscriptMsg("请先在商品管理中选择模版！", "");
                    return;
                }
                appWhere = Utils.getWhereByInfo(mgr.busnieseNum.ToString(), Session[DTKeys.SESSION_APP_INFO].ToString(), Session[DTKeys.SESSION_TEMPLATES_INFO].ToString());
                Model.sf_modeconfig modelConfig = new BLL.sf_modeconfig().GetModel("settingNum='" + Session[DTKeys.SESSION_TEMPLATES_INFO].ToString() + "'");
                if (modelConfig.marketing_type==1)
                {
                    dis = "";
                }
                //&&加盟商
                RptBind(appWhere+ CombSqlTxt(keywords), "addTime desc,id desc");
            }
        }

        #region 数据绑定
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            BLL.sf_user_info bll = new BLL.sf_user_info();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("cur_list.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (userNum like  '%")
                      .Append(_keywords)
                      .Append("%' or nickName like '%")
                      .Append(_keywords)
                      .Append("%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("cur_list_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_list.aspx", "keywords={0}", txtKeywords.Text));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("cur_list_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("cur_list.aspx", "keywords={0}", this.keywords));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("sys_customer_guanli", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.manager bll = new BLL.manager();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除客户" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("cur_list.aspx", "keywords={0}", this.keywords));
        }

        /// <summary>
        /// 获取 客户 的工资
        /// </summary>
        /// <param name="userNum">客户编号</param>
        public string getMoneyByNum(string userNum) 
        {
            BLL.sf_user_info userBll = new BLL.sf_user_info();
            BLL.sf_goods_order orderInfoBll = new BLL.sf_goods_order();
            BLL.sf_manager_scale scaleBll = new BLL.sf_manager_scale();
            Model.sf_user_info userInfo = userBll.GetModelByNum(userNum,appWhere);
            double money = 0;
            //一、二、三级订单提成
            double oneScale = 0, twoScale = 0, threeScale = 0;
            DataTable dsc = scaleBll.GetList(appWhere).Tables[0];
            foreach (DataRow r in dsc.Rows)
            {
                if (r["levels"].ToString() == "1")
                {
                    oneScale = Convert.ToDouble(r["scale"]);
                }
                else if (r["levels"].ToString() == "2")
                {
                    twoScale = Convert.ToDouble(r["scale"]);
                }
                else if (r["levels"].ToString() == "3")
                {
                    threeScale = Convert.ToDouble(r["scale"]);
                }
            }
            DataTable dt1 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 1, appWhere);
            foreach (DataRow r in dt1.Rows)
            {
                money += Convert.ToDouble(r["totalMoney"]) * oneScale;
            }
            DataTable dt2 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 2, appWhere);
            foreach (DataRow r in dt2.Rows)
            {
                money += Convert.ToDouble(r["totalMoney"]) * twoScale;
            }
            DataTable dt3 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 3, appWhere);
            foreach (DataRow r in dt3.Rows)
            {
                money += Convert.ToDouble(r["totalMoney"]) * threeScale;
            }
            return money.ToString();
        }

    }
}